package com.glodon.paas.document.dao.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.glodon.paas.document.api.bean.File;
import com.glodon.paas.document.constant.DocumentConstant;
import com.glodon.paas.document.service.util.DocumentBuilder;
import com.glodon.paas.util.BeanFactoryUtil;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;

@Entity(value = "file", noClassnameStored = true)
public class FileInfoEntity {

    public static DocumentBuilder builder = null;

    public static File getDto(FileInfoEntity e, boolean cascade) {
        if (e == null) {
            return null;
        } else {
            File d = new File();
            d.setAutoDeleted(e.getDeletedStatus() == DocumentConstant.FILE_STATUS_RECYCLE_CASCADE);
            d.setCreateTime(e.getCreateTime());
            d.setDataBagId(e.getDataBagId());
            d.setDeleted(e.isDeleted());
            d.setExtension(e.getExtension());
            d.setFolder(e.isFolder());
            d.setId(e.getId());
            d.setName(e.getName());
            d.setOwnerId(e.getOwnerId());
            d.setCreator(e.getCreator());
            d.setUpdator(e.getUpdator());
            d.setParentId(e.getParentId());
            d.setRevisionNumber(e.getActiveRevision());
            d.setShared(DocumentConstant.SHARE_TYPE_SHARED.intValue() == e.getShareType()
                        || DocumentConstant.SHARE_TYPE_SHARED_AND_PUBLISHED.intValue() == e.getShareType());
            d.setPublished(DocumentConstant.SHARE_TYPE_PUBLISHED.intValue() == e.getShareType()
                           || DocumentConstant.SHARE_TYPE_SHARED_AND_PUBLISHED.intValue() == e.getShareType());
            d.setSize(e.getSize());
            d.setUpdateTime(e.getUpdateTime());
            d.setCount(e.getCount());
            d.setFolderCount(e.getFolderCount());
            d.setProjectId(e.getProjectId());
            if (cascade) {
                if (builder == null) {
                    builder = BeanFactoryUtil.getBean(DocumentBuilder.class);
                }
                builder.buildUser(d);
                builder.buildToken(d);
                builder.buildLock(d, e.getLockId());
            }
            return d;
        }
    }

    public static List<File> getDtos(List<FileInfoEntity> es, boolean cascade) {
        if (es == null) {
            return null;
        } else {
            List<File> ds = new ArrayList<File>(es.size());
            for (FileInfoEntity e : es) {
                ds.add(getDto(e, cascade));
            }
            return ds;
        }
    }

    @Id
    private String  id;

    private String  parentId;

    // 文件或文件夹，参考 DocumentConstant.FILE_TYPE_FOLDER
    private Integer type;

    // 还未未使用
    private String  fileType;

    private String  ownerId;
    private String  updator;
    private String  creator;
    private String  name;

    // 不会是null，文件夹的扩展名为""
    private String  extension;

    private Long    size           = 0L;

    // 文件夹：表示 内部文件数； 文件：表示内部版本数
    private Integer count          = 0;

    private Integer deletedStatus  = 0;

    private Date    createTime;

    private Date    updateTime;

    // private boolean shared;

    // 0:not shared, 1: shared, 2:published, 3:shared+published
    private int     shareType;

    // 最后一个可用的版本号,为方便统一条件查询，文件夹时该值为1(非零)
    // 如查询有效文件列表时activeRevision > 0
    private Integer activeRevision = 0;

    private String  dataBagId;

    private String  lockId;
    private int     folderCount;

    public String getDataBagId() {
        return dataBagId;
    }

    public void setDataBagId(String dataBagId) {
        this.dataBagId = dataBagId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getFileType() {
        return fileType;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getExtension() {
        return extension;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Integer getDeletedStatus() {
        return deletedStatus;
    }

    public void setDeletedStatus(Integer deletedStatus) {
        this.deletedStatus = deletedStatus;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public boolean isShared() {
        return shareType == DocumentConstant.SHARE_TYPE_SHARED
               || shareType == DocumentConstant.SHARE_TYPE_SHARED_AND_PUBLISHED;
    }

    public boolean isPublished() {
        return shareType == DocumentConstant.SHARE_TYPE_PUBLISHED
               || shareType == DocumentConstant.SHARE_TYPE_SHARED_AND_PUBLISHED;
    }

    // public void setShared(boolean shared) {
    // this.shared = shared;
    // }

    public boolean isFolder() {
        return this.type.intValue() == DocumentConstant.FILE_TYPE_FOLDER.intValue();
    }

    public boolean isDeleted() {
        return this.deletedStatus.intValue() != DocumentConstant.FILE_STATUS_NORMAL.intValue();
    }

    public String getFullName() {
        if (this.extension != null && !"".equals(this.extension)) {
            return this.name + "." + this.extension;
        } else {
            return this.name;
        }
    }

    public int getShareType() {
        return shareType;
    }

    public void setShareType(int shareType) {
        this.shareType = shareType;
    }

    public String getLockId() {
        return lockId;
    }

    public void setLockId(String lockId) {
        this.lockId = lockId;
    }

    public String getUpdator() {
        return updator;
    }

    public void setUpdator(String updator) {
        this.updator = updator;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public int getFolderCount() {
        return folderCount;
    }

    public void setFolderCount(int folderCount) {
        this.folderCount = folderCount;
    }

    public Integer getActiveRevision() {
        return activeRevision;
    }

    public void setActiveRevision(Integer activeRevision) {
        this.activeRevision = activeRevision;
    }
    
    // 项目文件中的ownerId为 projectId，个人文档中的ownerId为UserId
    public String getProjectId(){
        if(this.ownerId != null && this.creator != null && !this.ownerId.equals(this.creator)){
            return this.ownerId;
        }
        else{
            return null;
        }
    }
    
    //根目录文件的parentId为ownerId
    public boolean isRootFile(){
        if(this.ownerId.equals(this.parentId)){
            return true;
        }
        else{
            return false;
        }
    }
}
